type listener = (...args: any[]) => void;

export function createEvent() {
  const listeners: listener[] = [];
  return {
    on: (cb: listener) => {
      listeners.push(cb);
    },
    off: (cb: listener) => {
      const index = listeners.indexOf(cb);
      if (index > -1) listeners.splice(index, 1);
    },
    emit: (val?: Parameters<listener>) => {
      listeners.forEach(listener => listener(val));
    }
  };
}
